import { useDidShow } from '@tarojs/taro';
// 全局样式
import './app.less';
import SystemConfigBusiness from './business/SystemConfigBusiness';
import systemStore from './stores/SystemStore';
import useSocket from './hooks/useSocket';
import useChat from './hooks/useChat';
import useIdentity from './hooks/useIdentity';
import { useEffect } from 'react';
import chatStore from './stores/ChatStore';
import { observer } from 'mobx-react-lite';

function App(props) {
  const { useConnectWebSocket, getWebSocketMessage } = useSocket();
  const { useInitChatSessionList } = useChat();
  const { getUserInfo } = useIdentity();
  const { senderId } = chatStore;

  // 连接 websocket
  useConnectWebSocket();
  // 初始化租客/房东的会话列表
  useInitChatSessionList();

  // 获取系统配置
  const initSystemConfig = async () => {
    const systemConfig = await SystemConfigBusiness.getSystemConfig();
    systemStore.setSystemConfig(systemConfig);
  };

  useDidShow(() => {
    initSystemConfig();
    getUserInfo();
    getWebSocketMessage();
  });

  useEffect(() => {
    getWebSocketMessage();
  }, [senderId]);

  return props.children;
}

export default observer(App);
